package com.yyfax.ph.personal.mymoney.cash;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.Loader;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.yyfax.common.indicator.PagerSlidingTabStrip;
import com.yyfax.common.tools.SystemTool;
import com.yyfax.common.tools.TimeUtils;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.statistics.StatService;
import com.yyfax.ph.R;
import com.yyfax.ph.StatIDSet;
import com.yyfax.ph.base.BaseYYFaxActivity;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.utils.FormatUtils;
import com.yyfax.ph.common.widgets.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 返现明细
 * Created by zhangxian on 2017/11/3.<br>
 * Copyright 2017 www.yylending.com. All Rights Reserved.<br><br>
 */
public class CashActivity extends BaseYYFaxActivity {

	@BindView(R.id.rebate_date_tv)
	TextView mRebateDateTv;
	@BindView(R.id.rebate_number_tv)
	TextView mRebateNumberTv;
	@BindView(R.id.rebate_amount_tv_tv)
	TextView mRebateAmountTvTv;
	@BindView(R.id.rebate_info_layout)
	LinearLayout mRebateInfoLayout;
	@BindView(R.id.with_draw_date_tv)
	TextView mWithDrawDateTv;
	@BindView(R.id.with_draw_number_tv)
	TextView mWithDrawNumberTv;
	@BindView(R.id.with_draw_amount_tv_tv)
	TextView mWithDrawAmountTvTv;
	@BindView(R.id.with_draw_info_layout)
	LinearLayout mWithDrawInfoLayout;
	@BindView(R.id.tab_layout)
	PagerSlidingTabStrip mTabLayout;
	@BindView(R.id.auto_view_pager)
	ViewPager mAutoViewPager;

	RebateDetailFragment mRebateFragment;
	DrawDetailFragment mWithDrawFragment;

	ArrayList<Fragment> mFragments = new ArrayList<>();
	List<String> mListTitles = new ArrayList<>();

	TimePickerView mTimePickerView;

	public static void start(Activity aty) {
		Intent intent = new Intent(aty, CashActivity.class);
		aty.startActivity(intent);
	}

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	public int getContentViewRes() {
		return R.layout.cash_activity;
	}

	public void setRebateInfo(int cnt, int amountSum) {
		mRebateNumberTv.setText(String.valueOf(cnt));
		mRebateAmountTvTv.setText(FormatUtils.getFormatMoney(amountSum));
	}

	public void setWithDrawInfo(int cnt, int amountSum) {
		mWithDrawNumberTv.setText(String.valueOf(cnt));
		mWithDrawAmountTvTv.setText(FormatUtils.getFormatMoney(amountSum));
	}

	@Override
	public void initContentView() {
		super.initContentView();
		setBtnRightVisible(true);
		setTitleLeftVisible(false);
		setTitleVisible(true);
		setTitle(R.string.with_draw_title);

		mListTitles.add(getResources().getString(R.string.rebate));
		mListTitles.add(getResources().getString(R.string.withdraw));
		mRebateFragment = new RebateDetailFragment();
		mWithDrawFragment = new DrawDetailFragment();

		mRebateDateTv.setText(SystemTool.getDataTime("yyyy年MM月"));
		mWithDrawDateTv.setText(SystemTool.getDataTime("yyyy年MM月"));

		mFragments.add(mRebateFragment);
		mFragments.add(mWithDrawFragment);
		mAutoViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mFragments, mListTitles));
		mTabLayout.setViewPager(mAutoViewPager);
		mAutoViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				onTabSelect(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {
			}
		});
		mTabLayout.setTypeface(null, Typeface.NORMAL);
	}

	private void refreshSelectedDate(Date date){
		Date currentDate = new Date();
		if (currentDate.getTime() < date.getTime()) {
			showToast(R.string.date_select_error);
		} else {
			String currentSelectTime = TimeUtils.getTimeWithFormat(date, TimeUtils.DATE_FORMAT_YEAR_MONTH_ZN);
			String currentStartTime = TimeUtils.getTimeWithFormat(date, TimeUtils.DATE_FORMAT_YEAR_MONTH_DAY_BACK_SLANT);
			mRebateDateTv.setText(currentSelectTime);
			mRebateFragment.setStartDate(currentStartTime);

			mWithDrawDateTv.setText(currentSelectTime);
			mWithDrawFragment.setStartDate(currentStartTime);
		}
	}

	private void showTimePickerView() {
		mTimePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH);
		mTimePickerView.setTime(new Date());
		mTimePickerView.setCyclic(false);
		mTimePickerView.setCancelable(true);
		// 时间选择后回调
		mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
			@Override
			public void onTimeSelect(Date date) {
				refreshSelectedDate(date);
			}
		});
		mTimePickerView.show();
	}

	@OnClick({R.id.rebate_date_tv, R.id.with_draw_date_tv})
	public void onClick(View view) {
		super.onClick(view);
		switch (view.getId()) {
			case R.id.rebate_date_tv:
			case R.id.with_draw_date_tv:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E008);
				showTimePickerView();
				break;
			default:
				break;
		}
	}

	public void onTabSelect(int position) {
		switch (position) {
			case 0:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E006);
				mRebateInfoLayout.setVisibility(View.VISIBLE);
				mWithDrawInfoLayout.setVisibility(View.GONE);
				break;
			case 1:
				StatService.onEvent(YJPH.getInstance(), StatIDSet.EVENT_ID_E007);
				mRebateInfoLayout.setVisibility(View.GONE);
				mWithDrawInfoLayout.setVisibility(View.VISIBLE);
				break;
		}
	}

	static class MyPagerAdapter extends FragmentPagerAdapter {
		private List<Fragment> fragments;
		private List<String> titles;

		public MyPagerAdapter(FragmentManager fm, List<Fragment> mFragments, List<String> mTitles) {
			super(fm);
			fragments = mFragments;
			titles = mTitles;
		}

		@Override
		public int getCount() {
			return fragments.size();
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return titles.get(position);
		}

		@Override
		public Fragment getItem(int position) {
			return fragments.get(position);
		}
	}
}
